{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import print_function, division\n",
    "import os\n",
    "import sys\n",
    "import subprocess\n",
    "import shutil"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def class_process(dir_path, dst_dir_path, class_name, maxSize=1024):\n",
    "    class_path = os.path.join(dir_path, class_name)\n",
    "    if not os.path.isdir(class_path):\n",
    "        return\n",
    "\n",
    "    dst_class_path = os.path.join(dst_dir_path, class_name)\n",
    "    if not os.path.exists(dst_class_path):\n",
    "        os.makedirs(dst_class_path)\n",
    "\n",
    "    for file_name in os.listdir(class_path):\n",
    "        if '.avi' not in file_name:\n",
    "            continue\n",
    "        name, ext = os.path.splitext(file_name)\n",
    "        dst_directory_path = os.path.join(dst_class_path, name)\n",
    "\n",
    "        video_file_path = os.path.join(class_path, file_name)\n",
    "\n",
    "        # skip large files\n",
    "        # if os.path.getsize(video_file_path) > maxSize * 1000:\n",
    "        #\tcontinue\n",
    "\n",
    "        try:\n",
    "            if os.path.exists(dst_directory_path):\n",
    "                if not os.path.exists(os.path.join(dst_directory_path, 'image_00001.jpg')):\n",
    "                    subprocess.call('rm -r \\\"{}\\\"'.format(dst_directory_path), shell=True)\n",
    "                    print('remove {}'.format(dst_directory_path))\n",
    "                    os.makedirs(dst_directory_path)\n",
    "                else:\n",
    "                    continue\n",
    "            else:\n",
    "                os.makedirs(dst_directory_path)\n",
    "        except:\n",
    "            print(dst_directory_path)\n",
    "            continue\n",
    "\n",
    "        # cmd = 'ffmpeg -i \\\"{}\\\" -vf scale=-1:240 \\\"{}/image_%05d.jpg\\\"'.format(video_file_path, dst_directory_path)\n",
    "        cmd = 'ffmpeg -i \\\"{}\\\" -qscale:v 2 \\\"{}/image_%05d.jpg\\\"'.format(video_file_path, dst_directory_path)\n",
    "        \n",
    "        subprocess.call(cmd, shell=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remove ../data/UCF--101/split1/train/BabyCrawling/v_BabyCrawling_g12_c06\n"
     ]
    }
   ],
   "source": [
    "train_paths = ['../data/UCF101/split{}/train/'.format(i) for i in range(1,4)]\n",
    "test_paths = ['../data/UCF101/split{}/test/'.format(i) for i in range(1,4)]\n",
    "dst_train_paths = ['../data/UCF--101/split{}/train/'.format(i) for i in range(1,4)]\n",
    "dst_test_paths = ['../data/UCF--101/split{}/test/'.format(i) for i in range(1,4)]\n",
    "for i in range(3):\n",
    "    for class_name in os.listdir(train_paths[i]):\n",
    "        class_process(train_paths[i], dst_train_paths[i], class_name)\n",
    "    for class_name in os.listdir(test_paths[i]):\n",
    "        class_process(test_paths[i], dst_test_paths[i], class_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['1 ApplyEyeMakeup\\n', '2 ApplyLipstick\\n', '3 Archery\\n', '4 BabyCrawling\\n', '5 BalanceBeam\\n', '6 BandMarching\\n', '7 BaseballPitch\\n', '8 Basketball\\n', '9 BasketballDunk\\n', '10 BenchPress\\n', '11 Biking\\n', '12 Billiards\\n', '13 BlowDryHair\\n', '14 BlowingCandles\\n', '15 BodyWeightSquats\\n', '16 Bowling\\n', '17 BoxingPunchingBag\\n', '18 BoxingSpeedBag\\n', '19 BreastStroke\\n', '20 BrushingTeeth\\n', '21 CleanAndJerk\\n', '22 CliffDiving\\n', '23 CricketBowling\\n', '24 CricketShot\\n', '25 CuttingInKitchen\\n', '26 Diving\\n', '27 Drumming\\n', '28 Fencing\\n', '29 FieldHockeyPenalty\\n', '30 FloorGymnastics\\n', '31 FrisbeeCatch\\n', '32 FrontCrawl\\n', '33 GolfSwing\\n', '34 Haircut\\n', '35 Hammering\\n', '36 HammerThrow\\n', '37 HandstandPushups\\n', '38 HandstandWalking\\n', '39 HeadMassage\\n', '40 HighJump\\n', '41 HorseRace\\n', '42 HorseRiding\\n', '43 HulaHoop\\n', '44 IceDancing\\n', '45 JavelinThrow\\n', '46 JugglingBalls\\n', '47 JumpingJack\\n', '48 JumpRope\\n', '49 Kayaking\\n', '50 Knitting\\n', '51 LongJump\\n', '52 Lunges\\n', '53 MilitaryParade\\n', '54 Mixing\\n', '55 MoppingFloor\\n', '56 Nunchucks\\n', '57 ParallelBars\\n', '58 PizzaTossing\\n', '59 PlayingCello\\n', '60 PlayingDaf\\n', '61 PlayingDhol\\n', '62 PlayingFlute\\n', '63 PlayingGuitar\\n', '64 PlayingPiano\\n', '65 PlayingSitar\\n', '66 PlayingTabla\\n', '67 PlayingViolin\\n', '68 PoleVault\\n', '69 PommelHorse\\n', '70 PullUps\\n', '71 Punch\\n', '72 PushUps\\n', '73 Rafting\\n', '74 RockClimbingIndoor\\n', '75 RopeClimbing\\n', '76 Rowing\\n', '77 SalsaSpin\\n', '78 ShavingBeard\\n', '79 Shotput\\n', '80 SkateBoarding\\n', '81 Skiing\\n', '82 Skijet\\n', '83 SkyDiving\\n', '84 SoccerJuggling\\n', '85 SoccerPenalty\\n', '86 StillRings\\n', '87 SumoWrestling\\n', '88 Surfing\\n', '89 Swing\\n', '90 TableTennisShot\\n', '91 TaiChi\\n', '92 TennisSwing\\n', '93 ThrowDiscus\\n', '94 TrampolineJumping\\n', '95 Typing\\n', '96 UnevenBars\\n', '97 VolleyballSpiking\\n', '98 WalkingWithDog\\n', '99 WallPushups\\n', '100 WritingOnBoard\\n', '101 YoYo\\n']\n",
      "1 ApplyEyeMakeup\n",
      "\n",
      "['1', 'ApplyEyeMakeup']\n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'dic' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-9-498931af92ab>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      8\u001b[0m         \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mline\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      9\u001b[0m         \u001b[0mact\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlabel\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mline\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mline\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 10\u001b[0;31m         \u001b[0mdic\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mact\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlabel\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m: name 'dic' is not defined"
     ]
    }
   ],
   "source": [
    "import os\n",
    "with open('../data/ucfTrainTestlist/classInd.txt', 'r') as f:\n",
    "    lines = f.readlines()\n",
    "    print(lines)\n",
    "    for line in lines:\n",
    "        print(line)\n",
    "        line = line.strip().split(' ')\n",
    "        print(line)\n",
    "        act, label = line[1], line[0]\n",
    "        dic[act] = int(label) - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
